﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZF.WS.UserControls;
using GCELibrary.Current;
using System.Transactions;
using ZF.BLL;
using Newtonsoft.Json;
using ZF.BLL.Admin;
using ZF.Model;
namespace ZF.WS.Admin
{
    public partial class MTypeManage : MType
    {
    

        [PageArgument(Key = "id", CanNull = false, BindOperType = PageOperType.Edit)]
        public int MTId { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
    
            if (!IsPostBack)
            {
                if (this.OperType == PageOperType.AddNew)
                    Add();
                else if (this.OperType == PageOperType.Edit)
                    Edit();
            }
        }
        public void InitControls()
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {
                if (!IsPostBack)
                {
                   this.ddlParent.DataSource= dc.Dic_MerchandiseType.Where(p => p.ParentId == 0).Select(p =>
                        new
                        {
                            Key = p.MerchandiseTypeId,
                            Text = p.Name,
                        }).ToList();
                   this.ddlParent.DataBind();
                }
            }
            //this.ddlType.DataSource = DictionaryConfig.INewType;
            //this.ddlType.DataBind();
        }

        public void Add()
        {
            InitControls();

        }
        public void AddSubmit(object sender, EventArgs e)
        {
            using (var dc = SiteInfo.CreateDBDataContext())
            {
                var mtype = new ZF.Model.Dic_MerchandiseType()
                {
                    Name = this.tbName.Text.Trim(),
                    InfoTemplate = this.fckInfoTemplate.Value,
                    Param = JsonConvert.SerializeObject(JsonConvert.DeserializeObject<MTypeJsonModel[]>(this.tbParams.Text)),
                    ParentId=this.ddlParent.SelectedValue.Convert<int>(),
                };
                try
                {
                    dc.Dic_MerchandiseType.InsertOnSubmit(mtype);
                    dc.SubmitChanges();
                    ResetCache();
                }
                catch (Exception ex)
                {
                    throw WarnFactory.CreateWarn(ex);
                }
                string msgKey = CTitle;
                SiteInfo.InsertLog("添加" + msgKey, null);
                WarnMessageLink link = new WarnMessageLink(string.Format("成功添加一个{0}，点击确定按钮返回上一页", msgKey));
                link.AddWarnMessageLink(string.Format("继续添加{0}", msgKey),Request.Url.ToString());
                WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success, ListPage).Alert();
            }
        }

        public void Edit()
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {

                var mtype = dc.Dic_MerchandiseType.FirstOrDefault(p => p.MerchandiseTypeId == this.MTId);
                if (mtype == null)
                    throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);

                InitControls(); ;

                this.tbName.Text = mtype.Name;
                this.fckInfoTemplate.Value = mtype.InfoTemplate;
                this.tbParams.Text = mtype.Param;
                this.ddlParent.SelectedValue = mtype.ParentId.ToString();
                if (!string.IsNullOrEmpty(mtype.Param))
                {
                    this.lvParams.DataSource = JsonConvert.DeserializeObject<MTypeJsonModel[]>(mtype.Param);
                    this.lvParams.DataBind();
                }


            }
        }
        public void EditSubmit(object sender, EventArgs e)
        {

            using (var ts = new System.Transactions.TransactionScope())
            {
                using (var dc = BLL.SiteInfo.CreateDBDataContext())
                {
                    var mtype = dc.Dic_MerchandiseType.FirstOrDefault(p => p.MerchandiseTypeId == this.MTId);
                    if (mtype == null)
                        throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
                    mtype.Name = this.tbName.Text.Trim();
                    mtype.InfoTemplate = this.fckInfoTemplate.Value;
                    mtype.Param = JsonConvert.SerializeObject(JsonConvert.DeserializeObject<MTypeJsonModel[]>(this.tbParams.Text));
                    mtype.ParentId = this.ddlParent.SelectedValue.Convert<int>();
                    try
                    {
                        dc.SubmitChanges();
                        ts.Complete();
                        ResetCache();
                    }
                    catch (Exception ex)
                    {
                        throw WarnFactory.CreateWarn(ex);
                    }
                    string msgKey = CTitle;

                    SiteInfo.InsertLog("修改" + msgKey, null);
                    WarnMessageLink link = new WarnMessageLink(string.Format("成功添加一个{0}，点击确定按钮返回{0}列表", msgKey));
 
                    link.AddWarnMessageLink(string.Format("重新修改{0}", msgKey),Request.Url.ToString());
                    WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success,ListPage).Alert();

                }


            }

        }

    
    }

}
